﻿using System.Security.Claims;

using WXWorkSvc.Common.Data;
using WXWorkSvc.Common.Services;

namespace OpenPms.Controllers;
[Route("api/[controller]")]
[ApiController]
[SwaggerTag("获取 JWT Token", "https://openpms.yikett.com/AboutApiPms")]

public class TokenController : ControllerBase
{
    /// <summary>
    /// 获取 JWT Token
    /// </summary>
    /// <param name="db"></param>
    /// <param name="svc"></param>
    /// <param name="id">忆客科技提供的id</param>
    /// <param name="secret">忆客科技提供的密钥</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<string> Get([FromServices] WXDbContext db, [FromServices] JWTService svc, int id = 1, string secret = "8330836d-cdb9-48ad-8851-86f206d5baf5")
    {
        var item = await db.Jwts.Where(ii => ii.Id == id && ii.Secret == secret).Select(ii => new { ii.Name, ii.ExtId }).FirstOrDefaultAsync();
        if (item is null)
            return "";
        else
            return svc.CreateToken(new Claim(ClaimTypes.Name, item.Name), new Claim("ExtId", item.ExtId.ToString()));

    }
}
